/*
 * ethernet.h
 *
 *  Created on: May 25, 2018
 *      Author: yuantao
 */

#ifndef ETHERNET_H_
#define ETHERNET_H_

#include "init.h"
#include "sys/socket.h"
#include <iostream>
#include <list>

using namespace std;


#define NO_ERROR 0
#define BYTES_SEND_ONCE 1024
#define MAX_BYTES_RECV_ONCE 1024

typedef enum
{
	PROTOCOL_UDP,
	PROTOCOL_TCP
}IP_PROTOCOL;

typedef struct _clientInfo
{
     int fd;
     unsigned int ip;
     unsigned short port;
}clientInfo;


class CEthernet
{
private:
	int m_sockRemote;
	int m_sockLocal;
	connection_config m_stConnConfig;
    int m_isClient;// is client or server
    int m_ip_protocol;//is UDP or TCP

    pthread_t readUdpData_ThreadHandle;
    list<clientInfo> listClient;
public:
	void setConnectionConfig(connection_config * pstConnConfig);
    int startConnection(int isClient,int ip_protocol);
    int stopConnection(void);
    CEthernet();
    ~CEthernet();

    int recvUdpData(char * buffOut,unsigned int maxLengthIn,unsigned int * peerIp, unsigned short * peerPort);
    int sendUdpData(char * buffIn,unsigned int buffLen);

	int sendUdpData(char * buffIn,unsigned int buffLen,unsigned int peerIp, unsigned short peerPort);

    ThreadRunFlag recvThreadRunningFlag;
    int recvThreadExitVal;

    void joinRecvThread(void);

    void cleanTcpClients(void);
};

int startLocalUdp(CEthernet *pEthLocal);

void *threadFunc_recvUdpData(void *arg);

#endif /* ETHERNET_H_ */
